package com.itheima.service;

import com.itheima.common.R;
import com.itheima.mapper.OrderMapper;
import com.itheima.pojo.Order;
import com.itheima.pojo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author wuyiqiang
 * @2022/6/16 16:44
 */
@Service
public class OrdersService {

    @Autowired
    private OrderMapper orderMapper;


    /**
     * 通过订单号查询订单
     *
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    public R<Page> page(int page, int pageSize, String number, String beginTime, String endTime) throws ParseException {
        page = (page - 1) * pageSize;
        //获取满足number的所有数据
        List<Order> orders = orderMapper.page(page, pageSize, number);
        System.out.println(orders + "===============================orders");

        //新建集合
        List<Order> orderList = new ArrayList<>();
        //判断时间条件是否为空
        if (beginTime != null && endTime != null) {
            //类型转换
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            Date beginParse = sdf.parse(beginTime);
            System.out.println(beginParse + "parse==============================");
            String beginFormat = sdf.format(beginParse);
            System.out.println(beginFormat + "===========================");
            Date endParse = sdf.parse(endTime);
            System.out.println(endParse + "parse==============================");

            String endFormat = sdf.format(endParse);
            System.out.println(endFormat + "==================================");

            Long begin = Long.valueOf(beginFormat);
            Long end = Long.valueOf(endFormat);
            //对比时间
            for (Order order : orders) {
                //类型转化，Date类型转换为Integer 类型
                String formatBegin = sdf.format(order.getOrderTime());
                Long orderTime = Long.valueOf(formatBegin);

                String formatEnd = sdf.format(order.getCheckoutTime());
                Long ETime = Long.valueOf(formatEnd);

                if (orderTime > begin && ETime < end) {
                    orderList.add(order);
                    System.out.println(orderList + "==========================循环中的集合");
                }
            }
        } else {
            for (Order order : orders) {
                orderList.add(order);
            }
        }

        System.out.println(orderList + "===========================循环外的集合");
        Page page2 = new Page();
        //设置返回页的总条数
        page2.setTotal(orderList.size());
        //设置返回页的每页数量
        page2.setSize(pageSize);
        if (orderList != null) {
            //设置返回页返回类对象的当前页
            page2.setCurrent(page);
            //将获得到的数据写进返回类中
            page2.setRecords(orderList);
            return R.success(page2);
        } else {
            return R.error("没有数据了");
        }
    }


    /**
     * 修改状态
     *
     * @param status
     * @param id
     */
    public void status(Integer status, String id) {
        orderMapper.updateStatus(id, status);
    }

}
